package com.ihr360.payroll.service.payroll.convertor;

import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.IConvertor;
import com.ihr360.payroll.config.PayrollConfigEntity;
import com.ihr360.payroll.config.SalaryFieldEntity;
import com.ihr360.payroll.model.dto.salary.meta.PayrollConfig;
import com.ihr360.payroll.model.dto.salary.meta.SalaryField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class PayrollConfigConvertor implements IConvertor<PayrollConfigEntity, PayrollConfig> {
    @Autowired
    SalaryFieldConvertor salaryFieldConvertor;

    @Override
    public PayrollConfig convertEntityToDto(PayrollConfigEntity entity) {
        return null;
    }

    @Override
    public PayrollConfigEntity convertDtoToEntity(PayrollConfig dto) {
        PayrollConfigEntity retValue = new PayrollConfigEntity();
        retValue.setName(dto.getName());
        List<SalaryField> fields = dto.getFields();
        if (CollectionUtils.isEmpty(fields)) {
            return retValue;
        }
        List<SalaryFieldEntity>
                salaryFieldEntities = fields.stream().map(f -> {
            SalaryFieldEntity entity = salaryFieldConvertor.convertDtoToEntity(f);
            return entity;
        }).collect(Collectors.toList());
        retValue.setFields(salaryFieldEntities);
        return retValue;
    }
}